package com.zeyun.EasyOnlineShareCloud.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.util.SaResult;
import com.zeyun.EasyOnlineShareCloud.dto.req.LoginReqDTO;
import com.zeyun.EasyOnlineShareCloud.dto.req.RegisterReqDTO;
import com.zeyun.EasyOnlineShareCloud.dto.resp.LoginRespDTO;
import com.zeyun.EasyOnlineShareCloud.service.UserService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户控制器
 */
@RestController
@RequiredArgsConstructor
public class UserController {

	private final UserService userService;

	/**
	 * 用户登录
	 */
	@PostMapping("/v1/user/login")
	public SaResult login(@Valid @RequestBody LoginReqDTO requestParam) {
		LoginRespDTO result = userService.login(requestParam);
		return SaResult.data(result);
	}

	/**
	 * 用户注册
	 */
	@PostMapping("/v1/user/register")
	public SaResult register(@Valid @RequestBody RegisterReqDTO requestParam) {
		userService.register(requestParam);
		return SaResult.ok("注册成功");
	}

	/**
	 * 用户登出
	 */
	@GetMapping("/v1/user/logout")
	@SaCheckLogin
	public SaResult logout() {
		userService.logout();
		return SaResult.ok("注销成功");
	}
}
